/// <reference path="player.ts" />
/// <reference path="monster.ts" />


class LunarMonster implements IMonster {
    /* Properties */
    
    sprite;
    is_dead: boolean = false;
    stage: StageManager;
    orientation: number;

    /* Methods */
    constructor(stage_manager: StageManager, x:number, y:number) {
        var bmp = new createjs.SpriteSheet({
            images: ["../assets/img/entities/lunar_monster.png"],
            frames: { width: 96, height: 72, regX: 47, regY: 36 },
            animations: { walk: [0, 2, "walk", 0.3] }
        });
        
        this.stage = stage_manager;
        this.sprite = new createjs.Sprite(bmp);
        this.sprite.gotoAndPlay("walk");//animate
        this.sprite.shadow = new createjs.Shadow("#454", 0, 2, 1); //bobo shadow 
        this.sprite.name = "lunar_monster";
        this.sprite.direction = 90;
        this.sprite.vX = 4;
        this.sprite.x = x;
        this.sprite.y = y;
        this.sprite.scaleX = -1;//direction vers la gauche
        this.sprite.x = this.sprite.x + this.sprite.regX;
        this.sprite.currentFrame = 0;
        this.sprite.speed = 3;
        this.changeOrientation(Mouvement.MOVE_LEFT);
    }

    getEntity() { return this.sprite; }

    hitTest(entity : Player) {
        var pt = this.sprite.globalToLocal(entity.getPlayerEntity().x, entity.getPlayerEntity().y);
        if (this.sprite.hitTest(pt.x, pt.y))
            return true;
        else
            return false;
    }

    changeOrientation(orientation) {
        //if (this.is_jumping) return; //pas chg d'orientation 
        if (this.is_dead) return;

        if (orientation == Mouvement.MOVE_LEFT) {
            this.sprite.scaleX = -1;
            this.sprite.x = this.sprite.x + this.sprite.regX;
        } else if (orientation == Mouvement.MOVE_RIGHT) {
            this.sprite.scaleX = 1;
            this.sprite.x = this.sprite.x - this.sprite.regX;
        }
        //console.log(this.sprite.width, this.sprite.regX);
        this.orientation = orientation;
    }

    tick() {
        //console.log("a", Math.random() * 100);
       
        if ((this.sprite.x - this.sprite.spriteSheet._regX) < 0) this.changeOrientation(Mouvement.MOVE_RIGHT);//depassement gauche
        else if ((this.sprite.x + this.sprite.spriteSheet._regX) > this.stage.stage.canvas.width) this.changeOrientation(Mouvement.MOVE_LEFT);//depassement droite

        /*if(this.rng(0.00000005) == true)// 0.1% de chance de changement de vitesse
            this.sprite.speed = Math.random() * 10;*/

        if (this.orientation == Mouvement.MOVE_LEFT ) this.sprite.x -= this.sprite.speed;//dpl monstre
        else this.sprite.x += this.sprite.speed;
            
    }

    rng(percent) : boolean {
        if (percent < 0 || percent > 100) return false;

        if (percent < Math.random() * 100) return true;
        else return false;

    }
}








